{extend name="base" /}
{block name="body"}
<div class="layui-card">
    <div class="layui-card-body">
        <form action="{:request()->url()}" class="layui-form" method="post">
            <legend>客户资料</legend>
            <hr>
            {foreach name="field" item="r"}
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label {if $r.is_required == '1'}layui-form-required{/if}">{$r.title}</label>
                    <div class="layui-input-block">
                        {if isset($data)}
                            {:get_code($r,$data)}
                        {else}
                            {:get_code($r)}
                        {/if}
                    </div>
                </div>
            </div>
            {/foreach}
            <hr>
            <legend>联系人信息</legend>
            <hr>
            <div class="layui-form" lay-filter="freshform">
                <table class="layui-table" lay-filter="test">
                        <thead>
                        <tr>
                            <th>类型</th>
                            <th>姓名</th>
                            <th>手机</th>
                            <th>邮箱</th>
                            <th>微信</th>
                            <th style="text-align: center"><button type="button" class="layui-btn layui-btn-sm add-btn">添加</button></th>
                        </tr>
                        </thead>
                        {if isset($data)}
                        {foreach name="data.crmcontact" key="i" item="cc"}
                        <tbody>
                            <tr>
                                <td>
                                    <input type="hidden"  name="contact[id][]" value="{$cc.id|default=''}">
                                    <select name="contact[type_id][]" lay-verify="required">
                                        <option value="">请选择类型</option>
                                        {foreach name="contact" key="k" item="v"}
                                        <option value="{$k}" {if condition="isset($data) and $cc.type_id eq $k"}selected="selected"{/if}>{$v}</option>
                                        {/foreach}
                                    </select>
                                </td>
                                <td>
                                    <input type="text"  name="contact[name][]" value="{$cc.name|default=''}" class="layui-input" autocomplete="off" placeholder="" lay-verify="required">
                                </td>
                                <td>
                                    <input type="text" name="contact[mobile][]" value="{$cc.mobile|default=''}" class="layui-input" autocomplete="off" placeholder="" lay-verify="required">
                                </td>
                                <td>
                                    <input type="text" name="contact[email][]" value="{$cc.email|default=''}" class="layui-input" autocomplete="off">
                                </td>
                                <td>
                                    <input type="text" name="contact[wechat][]" value="{$cc.wechat|default=''}" class="layui-input" autocomplete="off">
                                </td>
                                <td style="text-align: center"><button type="button" class="layui-btn layui-btn-sm layui-btn-danger btn-del" href="{:url('crm/contact/del',['id'=>$cc.id])}" lay-event="del">删除</button></td>
                            </tr>
                        </tbody>
                        {/foreach}
                        {/if}
                        <tbody class="addlists">
                            <tr>
                                <td>
                                    <select name="contact[type_id][]" lay-verify="required">
                                        <option value="">请选择类型</option>
                                        {foreach name="contact" key="k" item="v"}
                                        <option value="{$k}" {if condition="isset($data) and $data.type_id eq $k"}selected="selected"{/if}>{$v}</option>
                                        {/foreach}
                                    </select>
                                </td>
                                <td>
                                    <input type="text"  name="contact[name][]" class="layui-input" autocomplete="off" placeholder="" lay-verify="required">
                                </td>
                                <td>
                                    <input type="text" name="contact[mobile][]" class="layui-input" autocomplete="off" placeholder="" lay-verify="required">
                                </td>
                                <td>
                                    <input type="text" name="contact[email][]" class="layui-input" autocomplete="off">
                                </td>
                                <td>
                                    <input type="text" name="contact[wechat][]" class="layui-input" autocomplete="off">
                                </td>
                                <td style="text-align: center"><button type="button" class="layui-btn layui-btn-sm layui-btn-danger btn-del" lay-event="del">删除</button></td>
                            </tr>
                        </tbody>
                </table>
            </div>
            <hr>
            <legend>客服信息</legend>
            <hr>
            {foreach name="kefu" key="v" item="r"}
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">{$r}</label>
                    <div class="layui-input-block">
                        <select name="kefu[user_id][]" lay-search>
                            <option value="">请选择</option>
                            {foreach name="user_list" key="uv" item="u"}
                            <?php 
                                if(isset($data)){
                                    $this_user_id = \app\crm\model\CrmUser::where(['customer_id'=>$data['id'],'type_id'=>$v,'status'=>'1'])->value('user_id');
                                }else{
                                    $this_user_id = '';
                                }
                            ?>
                            <option value="{$u.id}" {if isset($data) and $this_user_id == $u.id}selected{/if}>{$u.name}</option>
                            {/foreach}
                        </select>
                        <input name="kefu[type_id][]" value="{$v}" type="hidden">
                    </div>
                </div>
            </div>
            {/foreach}
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-filter="i" lay-submit="">保存</button>
                    <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}
{block name="js"}
<script src="/static/js/jquery.min.js"></script>
    {foreach name="field" item="r"}
        {:get_code_js($r)}
    {/foreach}
<script>
$(function () {
    let form = layui.form
        ,laydate = layui.laydate,
        laypage = layui.laypage,
        layer = layui.layer,
        table = layui.table;
    //常规用法
    laydate.render({
        elem: '#test1'
    });
    laydate.render({
        elem: '#test2'
    });
    let i=1,j=1,k=1;
    $('.add-btn').click(function () {
        i++;
        addstrs1(i);
        form.render();
    });

    $('body').on("click",".btn-del",function () {
         var pre = $(this);
         var url = $(this).attr('href');
        layer.confirm('确定要删除么？',{
                btn:['确定','取消']
        },function () {
            $(pre).parent().parent().remove();
            if(url){
                $.ajax({
                    url: url,
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if (result.code === 1 && result.url != '') {
                            setTimeout(function () {
                                location.href = result.url;
                            }, 1000);
                        }
                        layer.closeAll('dialog');
                        layer.msg(result.msg);
                    },
                    error: function (xhr, state, errorThrown) {
                        layer.closeAll('dialog');
                        layer.msg(state + '：' + errorThrown);
                    }
                });
            }else{
                layer.closeAll('dialog');
            }
            
        })

        //
    });


    function getRandomNum() {
        return parseInt(Math.random()*50);
    }
    function addstrs1(i) {
        let  iNums = getRandomNum();
        let strs1;
             strs1 = '<tr>\n' +
                 '                            <td>\n' +
                 '                                <select name="contact[type_id][]" lay-filter="" lay-verify="required">\n' +
                 '                                    <option value="">请选择类型</option>\n' +
                                                       {foreach name="contact" key="k" item="v"}
                 '                                     <option value="{$k}" >{$v}</option>\n' +
                                                        {/foreach}
                 '                                </select>\n' +
                 '                            </td>\n' +
                 '                            <td>\n' +
                 '                                <input type="text"  name="contact[name][]" class="layui-input" autocomplete="off" lay-verify="required">\n' +
                 '                            </td>\n' +
                 '                            <td>\n' +
                 '                                <input type="text" name="contact[mobile][]" class="layui-input" autocomplete="off" lay-verify="required">\n' +
                 '                            </td>\n' +
                 '                            <td>\n' +
                 '                                <input type="text" name="contact[email][]" class="layui-input" autocomplete="off">\n' +
                 '                            </td>\n' +
                 '                            <td>\n' +
                 '                                <input type="text" name="contact[wechat][]" class="layui-input" autocomplete="off">\n' +
                 '                            </td>\n' +
                 '                            <td style="text-align: center"><button type="button" class="layui-btn layui-btn-sm layui-btn-danger btn-del">删除</button></td>\n' +
                 '                        </tr>';
        $('.addlists').append(strs1);
        //重新渲染
        laydate.render({
            elem: '#test1'+iNums
            ,trigger: 'click' //采用click弹出
        });
        laydate.render({
            elem: '#test2'+iNums
            ,trigger: 'click' //采用click弹出
        });
    }
})
</script>
{/block}